package cn.zch.soft.util.qrcode.img;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;

import cn.zch.soft.util.qrcode.utils.Helper;
import com.google.zxing.common.BitMatrix;
public final class MyMatrixToImageWriter {
  /**
   * 添加字体的属性设置
   */
  private static final Font font = new Font("微软雅黑", Font.TRUETYPE_FONT, 40);
  private static final MyMatrixToImageConfig DEFAULT_CONFIG = new MyMatrixToImageConfig();
  private MyMatrixToImageWriter() {
  }
  /**
   * Renders a {@link BitMatrix} as an image, where "false" bits are rendered as white, and "true" bits are rendered as black.
   */
  public static BufferedImage toBufferedImage(BitMatrix matrix) {
    return toBufferedImage(matrix, DEFAULT_CONFIG);
  }
  /**
   * As {@link #toBufferedImage(BitMatrix)}, but allows customization of the output.
   */
  public static BufferedImage toBufferedImage(BitMatrix matrix, MyMatrixToImageConfig config) {
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    BufferedImage image = new BufferedImage(width, height, config.getBufferedImageColorModel());
    int onColor = config.getPixelOnColor();
    int offColor = config.getPixelOffColor();
    for (int x = 0; x < width; x++) {
      for (int y = 0; y < height; y++) {
        image.setRGB(x, y, matrix.get(x, y) ? onColor : offColor);
      }
    }
    return image;
  }
  /**
   * Writes a {@link BitMatrix} to a file.
   *
   * @see #toBufferedImage(BitMatrix)
   */
  public static void writeToFile(BitMatrix matrix, String format, String imgPath, String logoPath, int i) throws IOException {
    writeToFile(matrix, format, imgPath, DEFAULT_CONFIG, logoPath, i);
  }
  /**
   * As {@link (BitMatrix, String, File)}, but allows customization of the output.
   */
  public static void writeToFile(BitMatrix matrix, String format, String imgPath, MyMatrixToImageConfig config, String logoPath, int i) throws IOException {
    BufferedImage image = toBufferedImage(matrix, config);
    if (!Helper.isNull(logoPath)) {
      // 添加logo
      overlapImage(matrix, image, imgPath, logoPath, i);
    }
    else {
      Graphics g = image.createGraphics();
      g.drawImage(image, 0, 0, matrix.getWidth(), matrix.getHeight(), null);
      g.setFont(font);
      g.setColor(Color.BLACK);
      String picTitle = "NO." + i;
      int txtLocateX = matrix.getWidth() / 2 - (String.valueOf(i).length() * 10) - 38;
      //int txtLocateX = (matrix.getWidth()-(picTitle.length()*20)+15)/2;
      int txtLocateY = 35;
      g.drawString(picTitle, txtLocateX, txtLocateY);
      g.dispose();
      ImageIO.write(image, format, new File(imgPath));
    }
  }
  /**
   * As {@link (BitMatrix, String, File)}, but allows customization of the output.
   */
  public static void writeToFiles(BitMatrix matrix, String format, String imgPath, MyMatrixToImageConfig config, String logoPath, int i) throws IOException {
    BufferedImage image = toBufferedImage(matrix, config);
    if (!Helper.isNull(logoPath)) {
      // 添加logo
      overlapImage(matrix, image, imgPath, logoPath, i);
    }
    else {
      ImageIO.write(image, format, new File(imgPath));
    }
  }
  /**
   * 二维码添加自定义logo
   */
  public static void overlapImage(BitMatrix matrix, BufferedImage image, String imgPath, String logoPath, int i) {
    try {
      BufferedImage logo = ImageIO.read(new File(logoPath));
      // Graphics2D g = image.createGraphics();
      // int imgWidth = image.getWidth();
      // int imgHeight = image.getHeight() - 70;
      // int width = imgWidth / 5;
      // int height = imgHeight / 5;
      // int x = (imgWidth - width) / 2;
      // int y = (imgHeight - height) / 2;
      // g.drawImage(logo, x+2, y+34, width, height, null);
      // g.dispose();
      // ImageIO.write(image, "png", new File(imgPath));
      //BufferedImage image = toBufferedImage(matrix, config);
      Graphics g = image.createGraphics();
      //g.drawImage(image, 0,0,matrix.getWidth(), matrix.getHeight(),null);
      int imgWidth = image.getWidth();
      int imgHeight = image.getHeight() - 70;
      int width = imgWidth / 5;
      int height = imgHeight / 5;
      int x = (imgWidth - width) / 2;
      int y = (imgHeight - height) / 2;
      g.drawImage(logo, x + 2, y + 34, width, height, null);
      //System.out.println(matrix.getWidth()+"  宽度  "+ matrix.getHeight()+" " + (String.valueOf(i).length()*3));
      g.setFont(font);
      g.setColor(Color.BLACK);
      g.drawString("NO." + i, matrix.getWidth() / 2 - (String.valueOf(i).length() * 10) - 35, 46);
      g.dispose();
      ImageIO.write(image, "png", new File(imgPath));
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * Writes a {@link BitMatrix} to a stream.
   *
   * @see #toBufferedImage(BitMatrix)
   */
  public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
    writeToStream(matrix, format, stream, DEFAULT_CONFIG);
  }
  /**
   * As {@link #writeToStream(BitMatrix, String, OutputStream)}, but allows customization of the output.
   */
  public static void writeToStream(BitMatrix matrix, String format, OutputStream stream, MyMatrixToImageConfig config) throws IOException {
    BufferedImage image = toBufferedImage(matrix, config);
    if (!ImageIO.write(image, format, stream)) {
      throw new IOException("Could not write an image of format " + format);
    }
  }
}
